# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  login      :string(255)
#  password   :string(255)
#  mobile     :string(255)
#  active     :boolean          default(TRUE)
#  gender     :boolean
#  avatar_url :string(255)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class User < ApplicationRecord

  def admin?
    login == 'xyy'
  end

  def to_s
    self.name||self.login
  end

  def password=(pwd)
    super Digest::MD5.hexdigest(pwd)
  end

  def resign!
    self.update(active: false)
  end

  def self.find(*args)
    return Base::User.new(login: 'system') if args[0].to_i==0
    super(*args)
  end

  # 在model里面获取的当前用户
  def self.current
    Base::Session.current_user
  end

  def self.valid_users(params)
    p params
    self.where(login: params[:login], password: Digest::MD5.hexdigest(params[:password])).first
  end
end
